package com.zx.oa.controller;

import com.alibaba.fastjson.JSON;
import com.zx.oa.dao.ClassInfo.ClassInfoDao;
import com.zx.oa.dao.ClassInfo.ClassInfoDaoImpl;
import com.zx.oa.dao.census.CensusDao;
import com.zx.oa.dao.census.CensusDaolpml;
import com.zx.oa.entity.ClassInfo;
import com.zx.oa.service.ClassInfo.ClassInfoService;
import com.zx.oa.service.ClassInfo.ClassInfoServiceImpl;
import com.zx.oa.service.student.StudentService;
import com.zx.oa.service.student.StudentServiceImpl;
import com.zx.oa.utils.PageSupport;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/classServlet.do")
public class ClassInfoServlet extends HttpServlet {

    ClassInfoService classInfoService=new ClassInfoServiceImpl();
    StudentService studentService =new StudentServiceImpl();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String caozuo = request.getParameter("param");
        if ("cha".equals(caozuo)){
            chaClassInfoServlet(request,response);
        }else if ("add".equals(caozuo)){
            addClassInfoServlet(request,response);
        }else if("modify_cha".equals(caozuo)){
            modify_chaClassServlet(request,response);
        }else if ("modify_save".equals(caozuo)){
            modify_saveClassInfoServlet(request,response);
        }else if ("delete".equals(caozuo)){
            deleteClassInfoServlet(request,response);
        }else if ("delete_cha".equals(caozuo)){
            delete_chaClassInfoServlet(request,response);
        }else if("jl".equals(caozuo)){
            findClassInfoServlet(request,response);
        }
    }

    private void findClassInfoServlet(HttpServletRequest request, HttpServletResponse response) throws IOException {

        String Type = "";
        if(null != request.getParameter("major")){
            Type = request.getParameter("major");
        }

        List<ClassInfo> classInfoList = classInfoService.getClassInfo(Type);
        response.setContentType("text/html");
        String json = JSON.toJSONString(classInfoList);
        response.getWriter().print(json);

    }

    private void delete_chaClassInfoServlet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=utf-8");
        int classId = Integer.parseInt(request.getParameter("classId"));
        boolean getClassInfoCount =studentService.getClassInfoCount(classId);
        PrintWriter out= response.getWriter();
        out.print(getClassInfoCount);
        out.flush();
        out.close();
        response.sendRedirect("classServlet.do?param=cha");
    }

    private void deleteClassInfoServlet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        int classId = Integer.parseInt(request.getParameter("classId"));
        boolean flag = classInfoService.deleteClassInfo(classId);
        if (flag){
            response.sendRedirect("classServlet.do?param=cha");
        }
    }

    private void modify_saveClassInfoServlet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String name = request.getParameter("classname");
        String major = request.getParameter("major");
        int classId = Integer.parseInt(request.getParameter("classId"));
        boolean flog=classInfoService.modifyClassInfo(name,major,classId);
        if (flog){
            response.sendRedirect("classServlet.do?param=cha");
        }
    }

    private void modify_chaClassServlet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int classInfoId = Integer.parseInt(request.getParameter("classId"));
        ClassInfo classInfo = classInfoService.getClassInfo(classInfoId);
        request.setAttribute("classInfo",classInfo);
        request.getRequestDispatcher("jsp/classModify.jsp").forward(request,response);
    }

    private void addClassInfoServlet(HttpServletRequest request, HttpServletResponse response) throws IOException {

        String major = request.getParameter("major");
        String name = request.getParameter("className");
        boolean flag=classInfoService.addClassInfo(name,major);
        if (flag){
            response.sendRedirect("classServlet.do?param=cha");
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

    private void chaClassInfoServlet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String Type=null;
        CensusDao censusDaolpml = new CensusDaolpml();

        String queryType = request.getParameter("major");
        if(null!=queryType&&!"".equals(queryType)){
            Type=queryType;
        }
        String pageIndex= request.getParameter("pageIndex");
        if(pageIndex==null){
            //第一次访问查询的servlet的时候没有pageIndex，赋默认值1
            pageIndex="1";
        }
        int currentPageNo=1;//当前页号
        int pageSize= PageSupport.pageSizeVal;//页面尺寸复制
        currentPageNo=Integer.parseInt(pageIndex);
        List<ClassInfo> classInfoList = classInfoService.getClassInfo(Type,currentPageNo,pageSize);

        ClassInfoDao classInfoDao = new ClassInfoDaoImpl();
        int totalCount=classInfoDao.getClassInfoTotalCount(Type);

        //存储页面信息
        PageSupport pages=new PageSupport();
        //设置页面尺寸
        pages.setPageSize(pageSize);
        //设置页面总记录数（注意此方法计算得到总页面数量）
        pages.setTotalCount(totalCount);
        //获取到总的页数
        int totalPageCount=pages.getTotalPageCount();
        if(currentPageNo<1){
            currentPageNo=1;
        }else if(currentPageNo>totalPageCount){
            currentPageNo=totalPageCount;
        }
        //将当前页号存储到pages对象
        pages.setCurrentPageNo(currentPageNo);



        request.setAttribute("pages",pages);//分页信息
        request.setAttribute("classInfoList",classInfoList);
        request.setAttribute("MajorAll",censusDaolpml.getMajorAll());
        request.getRequestDispatcher("jsp/classinfoList.jsp").forward(request, response);

    }

}
